package com.Iconcat.system.controller;
import com.Iconcat.system.entity.MovieBoxOffice;
import com.Iconcat.system.entity.SystemSetting;
import com.Iconcat.system.response.Result;
import com.Iconcat.system.service.MovieBoxOfficeService;
import com.Iconcat.system.service.SystemSettingsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/movie-box-office")
@CrossOrigin
@Api(value = "票房模块", tags = "票房接口")
public class MovieBoxOfficeController {

    @Resource
    private MovieBoxOfficeService movieBoxOfficeService;

    @GetMapping("/list")
    public Result MovieBoxOfficeList(@RequestParam(required = true, defaultValue = "1") Integer current,
                                     @RequestParam(required = true, defaultValue = "6") Integer size) {
        Page<MovieBoxOffice> page = new Page<>(current, size);
        LambdaQueryWrapper<MovieBoxOffice> queryWrapper = new LambdaQueryWrapper<>();

        Page<MovieBoxOffice> movieBoxOfficePage = movieBoxOfficeService.page(page, queryWrapper);
        // 计算所有电影的 boxDesc 总和值
        Double totalBoxDesc = movieBoxOfficeService.getTotalBoxDesc()/ 10000.0;  // 转换成万为单位;
        // 构建返回的 JSON
        Map<String, Object> result = new HashMap<>();
        result.put("movies", movieBoxOfficePage.getRecords());
        result.put("totalBoxDesc", totalBoxDesc);
        return Result.ok().data(result);
    }

    @GetMapping("/{id}")
    public Result queryMovieBoxOfficeById(@PathVariable("id") Long movieId) {
        MovieBoxOffice movieBoxOffice = movieBoxOfficeService.getById(movieId);
        return Result.ok().data("boxOffice", movieBoxOffice);
    }

    @PostMapping("/updateById")
    public Result updateMovieBoxOfficeById(@RequestBody MovieBoxOffice movieBoxOffice) {
        Boolean success = movieBoxOfficeService.updateById(movieBoxOffice);
        if (success) {
            return Result.ok().message("修改成功");
        } else {
            return Result.error().message("修改失败");
        }
    }

    @PostMapping("/addBoxOffice")
    public Result addMovieBoxOffice(@RequestBody MovieBoxOffice movieBoxOffice) {
        try {
            movieBoxOfficeService.save(movieBoxOffice);
            return Result.ok();
        } catch (Exception e) {
            // 打印日志
            return Result.error();
        }
    }

    @PostMapping("/delete/id")
    public Result deleteMovieBoxOfficeById(@RequestParam("movieId") Long movieId) {
        Boolean success = movieBoxOfficeService.removeById(movieId);
        if (success) {
            return Result.ok().message("删除成功");
        } else {
            return Result.error().message("删除失败");
        }
    }

    @PostMapping("/deleteIds")
    public Result deleteMovieBoxOfficeByIds(@RequestParam("ids") Collection<Long> ids) {
        Boolean success = movieBoxOfficeService.removeByIds(ids);
        if (success == false) {
            return Result.error().message("删除失败或数据不存在");
        }
        return Result.ok().message("删除成功");
    }
}
